package com.mall.common.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mall.common.domain.manage.S_deptDomain;
import com.mall.common.enums.FieldType;
import com.mall.common.exception.NoticeException;
import com.mall.common.service.S_deptService;
import com.mall.common.util.ResponseUtils;
import com.mall.common.validation.annotation.RequiredFieldValidator;
import com.mall.common.validation.annotation.RequiredStringValidator;
import com.mall.common.validation.annotation.SpecialRegexFieldValidator;
import com.mall.common.validation.annotation.Validations;

/**
 * 后台Action类 - 部门
 */
@Controller
@RequestMapping("/admin/system/dept")
public class DeptController extends BaseAuthController {
	
	@Resource private S_deptService s_deptService;
	
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public String list() {
		return "/admin/system/dept/list";
	}
	
	@RequestMapping(value="/tree", method=RequestMethod.GET)
	public String tree(){
		return "/admin/system/dept/tree";
	}
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(){
		return "/admin/system/dept/main";
	}
	
	@RequestMapping(value="/treeData", method=RequestMethod.POST)
	@ResponseBody
	public List<S_deptDomain> treeData(){
		S_deptDomain s_dept = S_deptDomain.genRoot();
		List<S_deptDomain> depts = s_deptService.getAll();
		depts.add(s_dept);
		return depts;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.GET)
	public String add(Long dept_id, Model model){
		S_deptDomain s_dept = s_deptService.find(dept_id);
		model.addAttribute("s_dept", s_dept);
		buttonAuth("add", "/admin/system/dept/save*", model);
		buttonAuth("update", "/admin/system/dept/update*", model);
		buttonAuth("delete", "/admin/system/dept/delete*", model);
		return "/admin/system/dept/input";
	}
	
	@Validations(
			requiredStrings = {
					@RequiredStringValidator(fieldName="code",message="部门编码不能为空"),
					@RequiredStringValidator(fieldName="name",message="部门名称不能为空")
			},
			specials = {
					@SpecialRegexFieldValidator(fieldName="link_phone",fieldType=FieldType.mobileOrTelephone)
			}
	)
	@RequestMapping(value="/save", method=RequestMethod.POST)
	@ResponseBody
	public String save(S_deptDomain dept, HttpServletResponse response) throws Exception {
		if(dept.getCode().contains(".")){
			S_deptDomain upDept = s_deptService.get("code", StringUtils.substringBeforeLast(dept.getCode(), "."));
			dept.setUpDomain(upDept);
		}else{
			dept.setUpDomain(null);
		}
		if (!s_deptService.isUnique("code", null, dept.getCode())){
			throw new NoticeException("error.unique", new Object[]{"部门编码"});
		}
		s_deptService.persist(dept);
		return ResponseUtils.renderJSON(dept, response);
	}
	
	@RequestMapping(value="/update/{id}", method=RequestMethod.GET)
	public String update(@PathVariable Long id, Model model){
		S_deptDomain dept = s_deptService.find(id);
		model.addAttribute("dept", dept);
		buttonAuth("add", "/admin/system/dept/save*", model);
		buttonAuth("update", "/admin/system/dept/update*", model);
		buttonAuth("delete", "/admin/system/dept/delete*", model);
		return "/admin/system/dept/add";
	}

	@Validations(
			requiredFields = {
					@RequiredFieldValidator(fieldName="id", message= "部门不存在")
			},
			requiredStrings = {
					@RequiredStringValidator(fieldName="code",message="部门编码不能为空"),
					@RequiredStringValidator(fieldName="name",message="部门名称不能为空")
			},
			specials = {
					@SpecialRegexFieldValidator(fieldName="link_phone",fieldType=FieldType.mobileOrTelephone)
			}
	)
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public String update(S_deptDomain dept, HttpServletResponse response){
		if(dept.getCode().contains(".")){
			S_deptDomain upDept = s_deptService.get("code", StringUtils.substringBeforeLast(dept.getCode(), "."));
			dept.setUpDomain(upDept);
		}else{
			dept.setUpDomain(null);
		}
		if (!s_deptService.isUniqueById("code", dept.getId(), dept.getCode())){
			throw new NoticeException("error.unique", new Object[]{"部门编码"});
		}
		s_deptService.update(dept);
		return ResponseUtils.renderJSON(dept, response);
	}
	
	@Validations(
			requiredFields = {
					@RequiredFieldValidator(fieldName="ids",message="部门不存在")
			}
	)
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	public String delete(Long[] ids, HttpServletResponse response) {
		S_deptDomain persistent = s_deptService.find(ids[0]);
		s_deptService.remove(ids);
		return ResponseUtils.renderJSON(persistent, response);
	}
	
	@RequestMapping(value="/checkCode", method=RequestMethod.POST)
	public String checkCode(String oldValue, String code, HttpServletResponse response){
		if (s_deptService.isUnique("code", oldValue, code)) {
			return ResponseUtils.renderJSON("true", response);
		} else {
			return ResponseUtils.renderJSON("false", response);
		}
	}
}